package com.cwits.wifi.ui.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cwits.wifi.R;
import com.cwits.wifi.ui.view.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import static com.cwits.wifi.R.id.file_view_photoc;
import static com.cwits.wifi.R.id.file_view_photox;
import static com.cwits.wifi.R.id.file_view_videoc;
import static com.cwits.wifi.R.id.file_view_videox;

/**
 * A simple {@link Fragment} subclass.
 */
public class FileFragment extends Fragment implements View.OnClickListener {

    private View mView;
    private RelativeLayout mRlPhoto,mRlVideo;
    private CustomViewPager mViewPager;
    private TextView mTvPhoto,mTvVideo;
    private View mLinePhotoc,mLinePhotox,mLineVideoc,mLineVideox;
    private List<Fragment> mTabs = new ArrayList<Fragment>();
    private FragmentPagerAdapter mAdapter;
    public FileFragment() {
        // Required empty public constructor
    }
    /** Fragment当前状态是否可见 */
    protected boolean isVisible;


    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(getUserVisibleHint()) {
            isVisible = true;
            onVisible();
        } else {
            isVisible = false;
            onInvisible();
        }
    }

    public void onWiFiState(boolean isAutoDisabled,int WiFi_state)
    {
        switch(WiFi_state)
        {
            case 1://已连接上，可以使用
                //在这里处理相关与wifi有关的信息

                break;
            case 2://断开，无法使用
                break;
            case 3://正在连接
                break;
            case 4://已连接
                break;
        }
    }

    /**
     * 可见
     */
    protected void onVisible() {
        initData();
        mViewPager.setOverScrollMode(ViewPager.OVER_SCROLL_NEVER);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setNoScroll(true);

        mViewPager.setCurrentItem(0, false);
        mTvPhoto.setTextColor(getResources().getColor(R.color.index_text_select));
        mLinePhotox.setVisibility(View.GONE);
        mLinePhotoc.setVisibility(View.VISIBLE);
        mTvVideo.setTextColor(getResources().getColor(R.color.default_title_color));
        mLineVideoc.setVisibility(View.GONE);
        mLineVideox.setVisibility(View.VISIBLE);
    }


    /**
     * 不可见
     */
    protected void onInvisible() {}

    public void selectAll()
    {
        switch(mViewPager.getCurrentItem()){
            case 0:
                if(mTabs.get(0) instanceof VideoFragment)
                {
                    ((VideoFragment)(mTabs.get(0))).selectAll();
                }
                break;

            case 1:
                if(mTabs.get(1) instanceof PhotoFragment)
                {
                    ((PhotoFragment)(mTabs.get(1))).selectAll();
                }
                break;
        }
    }


    public void initSelectAllCheckBox()
    {
        ((VideoFragment)(mTabs.get(0))).initSelectAllCheckBox();
        ((PhotoFragment)(mTabs.get(1))).initSelectAllCheckBox();
    }

        @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_file, container, false);
        init();

        return mView;
    }

    private void init() {
        mRlPhoto = (RelativeLayout) mView.findViewById(R.id.file_rl_photo);
        mRlVideo = (RelativeLayout) mView.findViewById(R.id.file_rl_video);
        mViewPager = (CustomViewPager) mView.findViewById(R.id.file_viewpager);
        mTvPhoto = (TextView) mView.findViewById(R.id.file_tv_photo);
        mTvVideo = (TextView) mView.findViewById(R.id.file_tv_video);
        mLinePhotoc = (View) mView.findViewById(file_view_photoc);
        mLinePhotox = (View) mView.findViewById(file_view_photox);
        mLineVideoc = (View) mView.findViewById(file_view_videoc);
        mLineVideox = (View) mView.findViewById(file_view_videox);
    }


    /*
    * 定义viewpager
    */
    private void initData() {
        PhotoFragment tabFragment1 = new PhotoFragment();
        Bundle args = new Bundle();
        args.putString("title", "First Fragment!");
        tabFragment1.setArguments(args);

        VideoFragment tabFragment2 = new VideoFragment();
        Bundle args2 = new Bundle();
        args2.putString("title", "First2 Fragment!");
        tabFragment2.setArguments(args2);
        mTabs.add(tabFragment2);
        mTabs.add(tabFragment1);


        mAdapter = new FragmentPagerAdapter(this.getActivity().getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return mTabs.size();
            }

            // 初始化每个页卡选项
            @Override
            public Object instantiateItem(ViewGroup arg0, int position) {

                Fragment ff = (Fragment) super.instantiateItem(arg0, position);
                // ff.setThings(mTabs.get(position).getr,position);
                return ff;
            }

            @Override
            public Fragment getItem(int arg0) {
                return mTabs.get(arg0);
            }
        };


        mRlPhoto.setOnClickListener(this);
        mRlVideo.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.file_rl_photo:   //照片
                if(mViewPager.getCurrentItem() == 0)
                    return;
                if(mTabs.get(1) instanceof PhotoFragment)
                {
                    if(((PhotoFragment) (mTabs.get(1))).getIsSelect())
                        ((PhotoFragment) (mTabs.get(1))).selectAll();
                }
                mViewPager.setCurrentItem(0, false);
                mTvPhoto.setTextColor(getResources().getColor(R.color.index_text_select));
                mLinePhotox.setVisibility(View.GONE);
                mLinePhotoc.setVisibility(View.VISIBLE);
                mTvVideo.setTextColor(getResources().getColor(R.color.default_title_color));
                mLineVideoc.setVisibility(View.GONE);
                mLineVideox.setVisibility(View.VISIBLE);

                break;
            case R.id.file_rl_video:     //摄影
                if(mViewPager.getCurrentItem() == 1)
                    return;
                if(mTabs.get(0) instanceof VideoFragment)
                {
                    if(((VideoFragment) (mTabs.get(0))).getIsSelect())
                        ((VideoFragment) (mTabs.get(0))).selectAll();
                }
                mViewPager.setCurrentItem(1, false);
                mTvPhoto.setTextColor(getResources().getColor(R.color.default_title_color));
                mLinePhotox.setVisibility(View.VISIBLE);
                mLinePhotoc.setVisibility(View.GONE);
                mTvVideo.setTextColor(getResources().getColor(R.color.index_text_select));
                mLineVideoc.setVisibility(View.VISIBLE);
                mLineVideox.setVisibility(View.GONE);
                break;
        }
    }
}
